package com.genscript.ms.c.core.aop.advice;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@ControllerAdvice(annotations= RestController.class)
@ResponseBody
public class GlobalExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 通用异常
     */
    @ExceptionHandler(Exception.class)
    public Map<String, Object> handlerException(HttpServletRequest request, HttpServletResponse response, Exception e) {
        logger.error(e.getMessage(), e);
        return null;
    }

    /**
     * 文件上传异常
     */
    @ExceptionHandler(MultipartException.class)
    public Map<String, Object> handleMultipartException(MultipartException e) {
        logger.error("{}", e.getMessage());
        return null;
    }
}
